<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('设备端口列表')" />
    <th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" id="deviceId" name="deviceId" th:value="${deviceId}"/>
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>电费名称：</label>
                                <input type="text" name="goodsName"/>
                            </li>
                            <li>
                                <label>端口状态：</label>
                                <select name="useStatus" th:with="type=${@dict.getType('cargoStatus')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>启用状态：</label>
                                <select name="status" th:with="type=${@dict.getType('enableStatus')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-primary" onclick="replenish()" shiro:hasPermission="biz:cargo:edit">
                    <i class="fa fa-database"></i> 一键补满
                </a>
                <a class="btn btn-success btn-xs" onclick="refreshList()" shiro:hasPermission="biz:cargo:edit">
                    <i class="fa fa-refresh"></i> 刷新大屏电费列表
                </a>
                <a  class="btn btn-warning" onclick="closeItem()">
                    <i class="fa fa-reply-all"></i> 返回
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-table-editable-js" />

     <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('biz:cargo:edit')}]];
        var removeFlag = [[${@permission.hasPermi('biz:cargo:remove')}]];
        var useStatusDatas = [[${@dict.getType('cargoStatus')}]];
        var statusDatas = [[${@dict.getType('enableStatus')}]];
        var prefix = ctx + "biz/cargo";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/replenishment/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                sortName: "number",
                sortOrder: "asc",
                modalName: "设备端口",
                onEditableSave: onEditableSave,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'tenantId',
                    title: '租户标识',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, 'open');
                    }
                },
                {
                    field: 'deviceSn',
                    title: '设备号',
                    visible: false,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, 'open');
                    }
                },
                {
                    field: 'number',
                    title: '端口编号',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, 'open');
                    }
                },
                {
                    field: 'goodsName',
                    title: '电费名称',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, 'open');
                    }
                },
                {
                    field: 'goodsDiagram',
                    title: '电费图片',
                    formatter: function (value, row, index) {
                        return $.table.imageView(value, 500, 500);
                    }
                },
                {
                    field: 'goodsStock',
                    title: '当前库存',
                    editable : {
                        type : 'number',
                        min: '0',
                        title : '当前库存',
                        emptytext : "【当前库存】为空"
                    }
                },
                {
                    field: 'capacity',
                    title: '端口容量',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 10, 'open');
                    }
                },
                {
                    field: 'useStatus',
                    title: '端口状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(useStatusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="out(\'' + row.id + '\')"><i class="fa fa-share-square-o"></i> 充电测试</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        function onEditableSave (field, row, rowIndex, oldValue, $el) {
            console.log("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
            //处理库存
            if(field == 'goodsStock'){
                var capacity = row.capacity;
                var goodsStock = Number(row[field]);
                if(goodsStock > capacity){
                    $.modal.msgError("库存不能大于端口容量");
                    $.table.refresh();
                }else {
                    $.operate.post(prefix + "/edit", { "id": row.id, "goodsStock": goodsStock, "capacity": capacity  });
                }
            }
        }
        /**
         * 一键补满
         */
        function out(id){
            $.operate.get(prefix + "/out?id="+id);
        }
        /**
         * 一键补满
         */
        function replenish(){
            $.operate.get(prefix + "/replenish?deviceId="+$("#deviceId").val());
        }
        /**
         * 一键补满
         */
        function refreshList(){
            $.operate.get(prefix + "/refresh/list?deviceId="+$("#deviceId").val());
        }
        refreshList
    </script>
</body>
</html>